package com.coe3.handle;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.coe3.data.AllIntent;
import com.coe3.data.PathManager;
import com.daydr3am.thenation.Library;
import com.daydr3am.thenation.R;
import com.daydr3am.thenation.TheNationMain;

public class ItemLibrarySpecialAdapter extends ArrayAdapter< String > {
	
	private LayoutInflater mInflater = null;
	private Activity mActivity;
	
	public ItemLibrarySpecialAdapter( Activity context, List< String > objects, LayoutInflater inflater ) {
		super( context, R.layout.activity_library_item, objects );
		this.mActivity = context;
		this.mInflater = inflater;
	}

	private class ViewHolder {
		
		private View mRow;
		private ImageView cover = null;
		private TextView title = null;
		private ImageButton btn = null;

		public ViewHolder(View row) {
			mRow = row;
		}
		
		public ImageView cover() {
			if( cover == null ) {
				cover = (ImageView)mRow.findViewById( R.id.item_iv );
			}
			return cover;
		}
		
		public TextView title() {
			if(null == title){
				title = (TextView) mRow.findViewById( R.id.item_txt );
			}
			return title;
		}
		
		public ImageButton button() {
			if(null == btn){
				btn = (ImageButton) mRow.findViewById( R.id.item_btn );
			}
			return btn;
		}
	}
	
	public final class clickToOpenPDF implements View.OnClickListener {
		
		private String pathPDF;
		
		public clickToOpenPDF(String pathPDF) {
			// TODO Auto-generated constructor stub
			this.pathPDF = pathPDF;
		}
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AllIntent.openPDF(mActivity, pathPDF);
		}
	}
	
	public final class clickToDeletePDF implements View.OnClickListener {
		
		private String fileName;
		
		public clickToDeletePDF(String fileName) {
			// TODO Auto-generated constructor stub
			this.fileName = fileName;
		}
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			new DeleteFile().execute(fileName);
		}
	}
	
	public class DeleteFile extends AsyncTask<String, Void, Void>
	{
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			TheNationMain.getInstanceDownloadDialog(mActivity).show();
		}
		
		@Override
		protected Void doInBackground(final String... params) {
			// TODO Auto-generated method stub
			PathManager.DeleteRecursive(new File(PathManager.combineExtension(params[0], PathManager.Extension_PDF)));
			PathManager.DeleteRecursive(new File(PathManager.combineExtension(params[0], PathManager.Extension_JPG)));
			
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			TheNationMain.dismissDownload(mActivity);
			Library.getInstance().initial(mActivity);
		}
		
	}
	
	@Override
	public View getView( int position, View convertView, ViewGroup parent ) {
		ViewHolder holder = null;
		String fileName = getItem(position);
		
		convertView = mInflater.inflate(R.layout.activity_library_item, null);
		
		holder = new ViewHolder(convertView);
		
		Bitmap bm = BitmapFactory.decodeFile(PathManager.combineExtension(fileName, PathManager.Extension_JPG));
		holder.cover().setImageBitmap(bm);
		holder.cover().setOnClickListener(new clickToOpenPDF(PathManager.combineExtension(fileName, PathManager.Extension_PDF)));
		
		String[] separated = fileName.split("/");
		String title = separated[separated.length-1];
		holder.title().setText(title);
		
		holder.button().setOnClickListener(new clickToDeletePDF(fileName));
		
		return convertView;
	}
}
